package com.conero.yangsu.lib;

import java.util.HashMap;

// 接口反馈数据
public class FeedbackData extends HashMap<String, Object> {
    private static final String codeError = "error";             // 错误代码
    private static final String codeMsg = "msg";                 // 提示信息
    private static final String codeGotoUrl = "goto_url";        // 跳转地址
    private static final String codeWaitSec = "wait_sec";        // 返回的任意数据
    private static final String codeData = "data";

    public FeedbackData() {
        super.put(codeError, 0);
        super.put(codeMsg, "请求成功！");
    }

    public FeedbackData(int error, String msg) {
        super.put(codeError, error);
        super.put(codeMsg, msg);
    }

    // 包含数据的内容输出
    public FeedbackData(int error, String msg, Object data) {
        super.put(codeError, error);
        super.put(codeMsg, msg);
        if (data != null) {
            super.put(codeData, data);
        }
    }

    // 接口错误
    public static FeedbackData error(String msg) {
        return new FeedbackData(1, msg);
    }

    // 接口错误
    public static FeedbackData error(String msg, String goUrl, int waitSet) {
        FeedbackData fdd = new FeedbackData(1, msg);
        if (!goUrl.isEmpty()) {
            fdd.put(codeGotoUrl, goUrl);
            fdd.put(codeWaitSec, waitSet);
        }
        return fdd;
    }

    // 请求正确接口访问
    public static FeedbackData success(String msg) {
        return new FeedbackData(0, msg);
    }

    // 请求正确接口访问
    public static FeedbackData success(Object data, String msg, String goUrl, int waitSet) {
        FeedbackData fdd = new FeedbackData(0, msg);
        if (data != null) {
            fdd.put(codeData, data);
        }
        if (null != goUrl && !goUrl.isEmpty()) {
            fdd.put(codeGotoUrl, goUrl);
            fdd.put(codeWaitSec, waitSet);
        }
        return fdd;
    }

    // 请求正确接口访问
    public static FeedbackData success(Object data, String msg) {
        return FeedbackData.success(data, msg, null, 0);
    }

    public static FeedbackData success(HashMap<String, Object> data) {
        return FeedbackData.success(data, "数据请求成功！");
    }
}
